<template>
  <div class="InvitationCode" :style="'min-height: ' + docheight + 'px'">
    <div v-if="!showTimeOut&&showCont">
      <div class="codeUserCont">
        <div class="codeUserTitle">
          <div>需求大厅</div>
        </div>
        <div class="listNav">
          <div :class="navId==index?'navItem navActive':'navItem'" @click="navFn(index)" :key="index" v-for="(item,index) in navArr">{{item.val}}</div>
        </div>
        <div class="codeUserList">
          <div
            class="codeUserItem"
            :key="index"
            v-for="(item, index) in userArr"
          >
            <div class="topCont">
              <img :src="item.package_cover" />
              <div class="userInfo">
                <div class="userName">
                  <span>{{ item.package_name }}</span>
                  <span class="sexTag" v-if="item.cleaning_sex==0">不限</span>
                  <span class="sexTag" v-if="item.cleaning_sex==1">男生</span>
                  <span class="sexTag" v-if="item.cleaning_sex==2">女生</span>
                </div>
                <div class="priceCont">
                  <div class="priceAll">最低到手：¥{{ item.cleaning_commission }}</div>
                </div>
                <div class="userTime">发布时间：{{ item.create_time }}</div>
                <div class="userTime">预约时间：{{ item.service_time }}</div>
              </div>
            </div>
            <div class="underCont">
              <div class="goodsType">
                <div class="goodsTypeItem">{{ item.package_rule_name }}</div>
              </div>
              <div class="orderBtn sureBtn" @click="sureFn(index,item)">接单</div>
            </div>
            <div class="remarkVal">订单备注:{{ item.service_remark?item.service_remark:'无备注' }}</div>
            <div class="remarkVal">服务地址:{{ item.service_address }} {{ item.house_number }}</div>
            <div class="remarkVal">联系电话:{{ item.user_phone }}({{ item.user_name }})</div>
          </div>
          <!-- <div class="nodataTip" v-if="userArr.length == 0">
            暂无需求订单～
          </div> -->
          <nodata v-if="userArr.length == 0" text="暂无需求订单～"></nodata>
          <div class="nodataTip" v-if="userArr.length>0">{{ loadMoreText }}</div>
        </div>
      </div>
      <sub-load v-if="showLoad"></sub-load>
    </div>
    <toast
      v-model="showToast"
      position="middle"
      width="50%"
      type="text"
      :time="1000"
      >{{ showText }}</toast
    >
    <van-popup v-model:show="showMask" round position="bottom">
      <div class="orderMask">
          <div class="maskTitle">操作提示</div>
          <div class="maskTip">{{ maskTipCont }}</div>
          <saveBtn color="#3273f6" style="width: 100%;" :value="maskBtnCont" @saveClick="sureMask"></saveBtn>
      </div>
    </van-popup>
    <footcpm></footcpm>
    <getLoad v-if="!showCont"></getLoad>
    <getTimeOut v-if="showTimeOut"></getTimeOut>
  </div>
</template>

<script>
import { Toast } from "vux";
import SaveBtn from "@/components/public/saveBtn.vue";
import subLoad from "@/components/public/subLoad.vue";
import getLoad from "@/components/public/getLoad";
import getTimeOut from "@/components/public/getTimeOut";
import nodata from "@/components/public/nodata";
import footcpm from "@/components/footcpm";
import { orderList,sureOrder } from "@/http/api";
import bus from '@/assets/evnetBus.js'

export default {
  components: { nodata,getTimeOut, getLoad, subLoad, Toast, SaveBtn, footcpm},
  data() {
    return {
      showTimeOut: false,
      showCont: false,
      showLoad: false,
      showText: "",
      loadMoreText:'数据加载中～',
      showToast: false,
      showDate: false,
      userArr: [],
      page: 1,
      per_page: 5,
      last_page: 0,
      getting: false,
      showMask:false,
      activeItem:{},
      activeIndex:0,
      maskTipCont:'',
      maskBtnCont:'',

      navArr:[
        {val:'性别不限'},
        {val:'男生'},
        {val:'女生'},
      ],
      navId:0,
    };
  },
  props: {},
  methods: {
    sureMask(){
      this.orderFn()
    },
    navFn(index){
      this.navId = index
      this.page = 1
      this.last_page = 0
      this.userArr = []
      this.getData()
    },
    sureFn(index,item){
      this.maskBtnCont ='同意并接单'
      this.maskTipCont = '确认接单之后，请在预约服务时间之前提前到达预约地点并与客户联系,切勿爽约。客户联系电话将在预约服务时间前30分钟显示，请遵守平台服务规则！'
      this.showMask = true;
      this.activeItem = item
      this.activeIndex=  index
    },
    orderFn(){
      let item  = this.activeItem
      let data = {
        order_id: item.order_id,
      };
      this.showMask = false;
      if(!this.showLoad){
        this.showLoad = true;
        sureOrder(data).then(res => {
          this.showCont = true;
          if (res.data.code == 1) {
            this.showToast = true;
            this.showText = res.data.msg
            this.userArr.splice(this.activeIndex,1)
            bus.$emit('getWait')
          } else {
            this.showToast = true;
            this.showText = res.data.msg
          }
          this.showLoad = false
        }).catch(() => {
          this.showTimeOut = true;
          this.showCont = true;
          this.showLoad = false;
        });
      }
      

    },
    getData() {
      let data = {
        page: this.page,
        per_page: this.per_page,
        cleaning_sex:this.navId,
      };
      orderList(data)
        .then(res => {
          this.showCont = true;
          if (res.data.code == 1) {
            let arr = res.data.data.data
            for(let i=0;i<arr.length;i++){
              this.userArr.push(arr[i])
            }
            setTimeout(() => {
              this.getting = false
            }, 100);
            this.last_page = res.data.data.last_page
            if(this.page==this.last_page){
              this.loadMoreText = '到底啦～'
            }else{
              this.loadMoreText = '数据加载中~'
            }
          } else {
            this.showToast = true;
            this.showText = res.data.msg
          }
        })
        .catch(() => {
          this.showTimeOut = true;
          this.showCont = true;
        });
    },
    nextfn() {
      if (this.$route.path == "/home") {
        let h =
          document.documentElement.scrollHeight || document.body.scrollHeight;
        let wh =
          document.documentElement.clientHeight || document.body.clientHeight;
        let top = document.documentElement.scrollTop || document.body.scrollTop;
        let bottom = h - wh - top;
        if (bottom <= 50) {
          if (this.getting == false) {
            if (this.last_page > this.page) {
              this.getting = true;
              this.page++;
              this.getData();
            }
          }
        }
      }
    }
  },
  mounted() {
    window.addEventListener("scroll", this.nextfn);
  },
  created() {
    this.getData();
  }
};
</script>

<style scoped lang="less">
.InvitationCode {
  width: 100%;
  background-color: #f7f8fa;
  box-sizing: border-box;
  padding-bottom: 3.5rem;
  padding-top: 1rem;
  .codeUserCont {
    width: 92%;
    margin: 0 auto;
    background-color: #fff;
    padding: 0.75rem;
    box-sizing: border-box;
    border-radius: 0.25rem;
    .codeUserTitle {
      font-size: 0.8rem;
      font-weight: bold;
      padding: 0.75rem 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .timeBtn {
        font-weight: normal;
        font-size: 0.65rem;
      }
    }
    .listNav{
        width: 100%;
        display: flex;
        margin-top: 0.5rem;
        .navItem{
            width: 30%;
            text-align: center;
            background-color: white;
            border: 1px solid #3273f6;
            padding: 0.25rem 0;
            border-radius: 1rem;
            font-size: 0.6rem;
            margin-right: 0.5rem;
            color: #3273f6;
        }
        .navActive{
            background-color: #3273f6;
            color: white;
        }
    }
    .codeUserList {
      padding: 0.75rem 0;
      .codeUserItem {
        width: 100%;
        padding: 0.5rem 0 0.75rem;
        border-bottom: 1px solid #ebebeb;
        .topCont{
          display: flex;
          padding: 0.5rem 0;
          align-items: center;
          img {
            margin-right: 0.75rem;
            width: 4.5rem;
            border-radius: 5px;
        }
        .userInfo {
          width: 2.5rem;
          flex: 1;
          .userName {
            font-size: 0.7rem;
            font-weight: bold;
            color: #333;
            display: flex;
            justify-content: space-between;
            width: 100%;
            .sexTag {
              padding: 0.1rem 0.5rem;
              background-color: #F56C6C;
              font-size: 0.6rem;
              color: #fff;
              border-radius: 0.1rem;
              font-weight: normal;
            }
            .greenTag{
              background-color: #85CE61;
            }
            .blacktag{
              color: #666;
              background-color: #ebebeb;
            }
          }
          .priceCont{
            align-items: center;
            display: flex;
            font-size: 0.6rem;
            color: #666;
            margin-top: 0.3rem;
            .priceSub{
              margin-left: 0.75rem;
            }
          }
          .userTime {
            font-size: 0.6rem;
            margin-top: 0.25rem;
            color: #666;
          }
        }
        }
        .underCont{
          display: flex;
          align-items: center;
          justify-content: space-between;
          .goodsType{
            display: flex;
            align-items: center;
            justify-content: space-between;
            .goodsNumCont{
              display: flex;
              height: 1rem;
              line-height: 1rem;
              color: #999;
              font-size: 0.6rem;
              align-items: center;
            }
            .goodsTypeItem{
              padding:0.1rem 0.5rem;
              background-color: #3273f6;
              font-size: 0.6rem;
              height: 1rem;
              line-height: 1rem;
              border-radius: 0.75rem;
              color: white;
            }
          }
          .orderBtn{
            padding: 0.35rem 1.8rem;
            border: 1px solid #3273f6;
            color: #3273f6;
            font-size: 0.6rem;
            margin-left: 0.5rem;
            border-radius: 0.25rem;
          }
          .sureBtn{
            background-color: #3273f6;
            color: white;
          }
        }
        .remarkVal{
          color: #666;
          font-size: 0.6rem;
          margin-top: 0.5rem;
        }
        .redBtn{
          color: #F56C6C;
        }
      }
      .nodataTip {
        color: #666;
        font-size: 0.6rem;
        text-align: center;
        margin-top: 20px;
      }
    }
  }
  .orderMask{
    padding: 0.75rem;
    box-sizing: border-box;
    .maskTitle{
      font-size: 0.8rem;
      font-weight: bold;
      text-align: center;
    }
    .maskTip{
      margin-top: 0.5rem;
      line-height: 1.1rem;
    }
  }
}
</style>
<style>
.InvitationCode .van-cell::after {
  border-bottom: none;
}
</style>
